package com.medical.lsq.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medical.lsq.dao.SysDiseaseMapper;
import com.medical.lsq.entity.SysDisease;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service
@Transactional

public class DiseaseService {

    @Autowired
    private SysDiseaseMapper diseaseMapper;

    /**
     * 添加慢病分类信息
     * @param sysDisease
     */
    public void add(SysDisease sysDisease) {
        sysDisease.setCreateTime(new Timestamp(System.currentTimeMillis()));
        sysDisease.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        diseaseMapper.insertSelective(sysDisease);
    }

    /**
     * 根据主键ID删除一条信息
     * @param id
     */
    public void delete(Integer id) {diseaseMapper.deleteByPrimaryKey(id);}

    /**
     * 根据对象信息进行更新
     * @param sysDisease
     */
    public void edit(SysDisease sysDisease) {
        sysDisease.getUpdateTime(new Timestamp(System.currentTimeMillis()));
        diseaseMapper.updateByPrimaryKeySelective(sysDisease);
    }
    /**
     * 查询所有慢病分类信息，可以进行模糊查询，需要进行分页
     * @return 慢病分类信息列表
     */
    public PageInfo<SysDisease> getDiseaseList(Integer currentPage, Integer limit, String diseaseName) {
        //利用PageHelper分页查询 注意：这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
        PageHelper.startPage(currentPage, limit);
        List<SysDisease> list = diseaseMapper.getDiseaseList(diseaseName);
        PageInfo<SysDisease> pageInfo = new PageInfo<SysDisease>(list);
        return pageInfo;
    }
    /**
     * 通过主键查找一条慢病分类息
     * @param id
     * @return
     */
    public SysDisease getDisease(Integer id) {
        return diseaseMapper.selectByPrimaryKey(id);
    }
}
